﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;

namespace PremisPublisher.Common
{

    /*

    使用msbuild编译解决方案

    * 
    * 作者：张鹏 
    * 日期：2016.5
 
    */

    public class BuildSol
    {
        public static string GetEnvPath()
        {
            return System.Configuration.ConfigurationManager.AppSettings.Get("msbuild_path");
        }


        public static void Exec(string exepath, string args, Action<string> ac, Action ac_done)
        {
            System.Diagnostics.Process cmd = new System.Diagnostics.Process();
            cmd.StartInfo = new System.Diagnostics.ProcessStartInfo();
            cmd.StartInfo.FileName = exepath;
            cmd.StartInfo.Arguments = args;
            cmd.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            cmd.StartInfo.RedirectStandardOutput = true;
            cmd.StartInfo.UseShellExecute = false;
            cmd.StartInfo.CreateNoWindow = true;

            ThreadPool.QueueUserWorkItem(new WaitCallback(s =>
            {
                Process p = s as Process;
                p.Start();
                if (ac != null)
                    p.OutputDataReceived += (a, b) =>
                    {
                        ac(b.Data);
                    };
                p.BeginOutputReadLine();
                System.Threading.Thread.Sleep(10);
                p.WaitForExit();
                p.Close();
                if (ac_done != null) ac_done();
            }), cmd);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="slPath">解决方案路径</param>
        public static void ReBuild(string slPath, Action<string> ac, Action ac_done)
        {
            Exec(GetEnvPath(), slPath + " /t:build /m ", ac, ac_done);
        }



        /// <summary>
        /// 解析解决方案，分别编译其中的项目，同时分别制定输出目录
        /// </summary>       
        /// <param name="ac">过程回调</param>
        /// <param name="ac_done">结束回调</param>
        public static void BuildProjects(Action<string> ac, Action ac_done)
        {

            string output = System.Configuration.ConfigurationManager.AppSettings.Get("msbuild_output_path");

            var analyzer = ProjectsAnalyzer.GetInstance();
            var projects = analyzer.get_all_project_csproj();

            System.Threading.Tasks.Task.Factory.StartNew(() =>
            {
                projects.ForEach(p =>
                {
                    System.Diagnostics.Process cmd = new System.Diagnostics.Process();
                    cmd.StartInfo = new System.Diagnostics.ProcessStartInfo();
                    cmd.StartInfo.FileName = GetEnvPath();
                    cmd.StartInfo.Arguments = p + " /t:build /p:outputpath=" + output + " /p:CopyToPublishedApplications=true";
                    cmd.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                    cmd.StartInfo.RedirectStandardOutput = true;
                    cmd.StartInfo.UseShellExecute = false;
                    cmd.StartInfo.CreateNoWindow = true;

                    cmd.Start();
                    if (ac != null)
                        cmd.OutputDataReceived += (a, b) =>
                        {
                            ac(b.Data);
                        };
                    cmd.BeginOutputReadLine();
                    System.Threading.Thread.Sleep(10);
                    cmd.WaitForExit();
                    cmd.Close();

                });

                ac_done();
            });
        }
    }
}
